package com.sskj.lib.model.repository;


import android.arch.lifecycle.LiveData;
import android.text.TextUtils;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.bean.UserBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.model.room.UserDao;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import io.reactivex.schedulers.Schedulers;

import static com.sskj.lib.BaseHttpConfig.BASE_URL;

/**
 * Created by lv on 18-5-7.
 */
//@Singleton
public class UserRepository {

    public UserDao userDao;

    @Inject
    public UserRepository(@Named("UserDao") UserDao userDao) {
        this.userDao = userDao;
    }

    public LiveData<List<UserData>> getAllUser() {
        return userDao.getAll();
    }

    public void updateUser() {
        OkGo.<HttpData<UserBean>>post(BASE_URL + "/Home/Users/userinfo")
                .params("money_type", "pb")
                .execute(new JsonCallBack<HttpData<UserBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UserBean>> response) {
                        HttpData<UserBean> httpData = response.body();
                        if (httpData.getStatus() == BaseHttpConfig.OK) {
                            UserBean myUserData = httpData.getData();
//                            UserData userData = new UserData();
//                            userData.setId(007);
//                            userData.setBalance_cny(myUserData.getBalance_cny());
//                            userData.setBalance_pb(myUserData.getBalance_pb());
//                            userData.setRealname(myUserData.getRealname());
//                            userData.setNickname(myUserData.getNickname());
//                            userData.setUpic(myUserData.getUpic());
//                            userData.setReg_time(myUserData.getReg_time());
//                            userData.setHas_tpwd(myUserData.isHas_tpwd());
//                            userData.setMobile(myUserData.getList().get(0).getMobile());
//                            userData.setZuigao(myUserData.getList().get(0).getZuigao());
//                            userData.setLeiji(myUserData.getList().get(0).getLeiji());
//                            userData.setLoss(myUserData.getList().get(0).getLoss());
//                            userData.setRanking(myUserData.getList().get(0).getRanking());
//                            userData.setNum(myUserData.getList().get(0).getNum());
//                            userData.setIncome(myUserData.getList().get(0).getIncome());
//                            insert(userData);
                        }
                    }
                });
//        }
    }


    public void insert(UserData user) {
        Schedulers.newThread().scheduleDirect(() -> userDao.insert(user));
    }

    public void clear() {
        Schedulers.newThread().scheduleDirect(() -> userDao.deleteAll());
    }

    public void update(UserData user) {
        Schedulers.newThread().scheduleDirect(() -> userDao.update(user));
    }

    public void update() {
        updateUser();
    }
}
